// src-electron/main.js
const { app, BrowserWindow, Menu, screen } = require("electron");
const { join } = require("path");

// 屏蔽安全警告
// ectron Security Warning (Insecure Content-Security-Policy)
process.env["ELECTRON_DISABLE_SECURITY_WARNINGS"] = "true";

let setWd = 500;

// 创建浏览器窗口时，调用这个函数。
const createWindow = () => {
  const { width, height } = screen.getPrimaryDisplay().workAreaSize;
  const win = new BrowserWindow({
    width: setWd,
    height: setWd * 1.78,
    frame: false,
    // fullscreen: true,
    webPreferences: {
      nodeIntegration: false,   // 禁用 Node.js 集成
      contextIsolation: true,   // 启用上下文隔离
      enableRemoteModule: true,
      webviewTag: true // 允许使用 webview 标签
      // preload: path.join(__dirname, 'preload.js')  // 你可以使用预加载脚本来控制iframe行为
    }
  });

  // win.setResizable(false);
  // win.loadURL('http://localhost:3000')
  // development模式
  if (process.env.VITE_DEV_SERVER_URL) {
    win.loadURL(process.env.VITE_DEV_SERVER_URL);
    // 开启调试台
    win.webContents.openDevTools();
  } else {
    win.loadFile(join(__dirname, "../dist/index.html"));
  }
};
// Menu.setApplicationMenu(null)

// Electron 会在初始化后并准备
app.whenReady().then(() => {
  createWindow();
  app.on("activate", () => {
    if (BrowserWindow.getAllWindows().length === 0) createWindow();
  });
});

app.on("window-all-closed", () => {
  if (process.platform !== "darwin") app.quit();
});
